//
//  CategoryIconLocalData.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 31.03.25.
//

import Foundation
class CategoryIconLocalData {
    
    static var linearMapList = CategoryIconLinearLocalData.iconMapList
    static var emojiMapList = CategoryIconEmojiLocalData.emojiMapList
    
    static func linearGroupTitles() -> Array<CategoryGroupTitle> {
        return findGroupTitles(iconData: linearMapList, type: .linearicon)
    }
    
    static func emojiGroupTitles() -> Array<CategoryGroupTitle> {
        return findGroupTitles(iconData: emojiMapList, type: .emojiicon)
    }
    
    static func findGroupTitles(iconData:[(name:String,data:Array<CategoryIcon>)], type: CategoryIconType) -> Array<CategoryGroupTitle> {
        var result = Array<CategoryGroupTitle>()
        for (index, group) in iconData.enumerated() {
            // 默认选中第一个
            result.append(CategoryGroupTitle(id: index, type: type,  name: group.name, isSelected: index == 0))
        }
        return result
    }
    
    static func getSelectedIconList(title: CategoryGroupTitle?, searchQuery: String? = nil) -> Array<CategoryIcon> {
        if (title == nil) {
            return Array()
        }
        let type = title!.type
        switch type {
        case .linearicon:
            if (searchQuery != nil && searchQuery?.isEmpty == false) {
                return linearMapList.first(where: { item in item.name == title?.name})?.data.filter { (icon) -> Bool in return
                    icon.name.contains(searchQuery!)
                } ?? Array()
            } else {
                return linearMapList.first(where: { item in item.name == title?.name})?.data ?? Array()
            }
        case .emojiicon:
            if (searchQuery != nil && searchQuery?.isEmpty == false) {
                return emojiMapList.first(where: { item in item.name == title?.name})?.data.filter { (icon) -> Bool in return
                    icon.name.contains(searchQuery!)
                } ?? Array()
            } else {
                return emojiMapList.first(where: { item in item.name == title?.name})?.data ?? Array()
            }
        }
    }
    
    static func findIcon(iconType: CategoryIconType, iconUnified:String) -> (name: String, icon: CategoryIcon)? {
        switch iconType {
        case .linearicon:
            return findIconFromData(data: linearMapList, iconUnified: iconUnified)
        case .emojiicon:
            return findIconFromData(data: emojiMapList, iconUnified: iconUnified)
        }
    }
    
    static private func findIconFromData(data: [(name:String,data:Array<CategoryIcon>)], iconUnified: String) -> (name:String, icon:CategoryIcon)? {
        for item in data {
            for iconItem in item.data {
                if iconItem.iconUnified.lowercased() == iconUnified.lowercased() {
                    return (name:item.name, icon:iconItem)
                }
            }
        }
        return nil
    }
    
    static func searchLinear(query: String) -> [(name:String,data:Array<CategoryIcon>)]{
        return searchIconData(iconData: linearMapList, query: query)
    }
    
    static func searchEmoji(query: String) -> [(name:String,data:Array<CategoryIcon>)]{
        return searchIconData(iconData: emojiMapList, query: query)
    }
    
    private static func searchIconData(iconData: [(name:String,data:Array<CategoryIcon>)], query: String) -> [(name:String,data:Array<CategoryIcon>)]{
        var result = [(name:String,data:Array<CategoryIcon>)]()
        for item in iconData {
            let filterResult = item.data.filter { (icon) -> Bool in return
                icon.name.contains(query)
            }
            if (!filterResult.isEmpty) {
                result.append((name:item.name,data:filterResult))
            }
        }
        return result
    }
}
